//
//  DrugAttentionViewController.m
//  安好
//
//  Created by 马晓良 on 14-12-4.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "DrugAttentionViewController.h"
#import "DrugDetailViewController.h"
#import "DrugDViewController.h"
@interface DrugAttentionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *encyArray;
@property (strong, nonatomic) IBOutlet UITableView *drugTableView;

@end

@implementation DrugAttentionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviBarAttribute];
    self.navigationItem.title = @"药品";
    self.encyArray = [NSMutableArray array];
    self.drugTableView.dataSource = self;
    self.drugTableView.delegate = self;
    
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - 设置naviBar 并实现左右naviBar button方法 -
- (void)setNaviBarAttribute{
    /**
     *  设置左NaviBar和右naviBar
     */
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 23, 23);
    [leftButton setImage:[UIImage imageNamed:@"公用_返回"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
//    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    rightButton.frame = CGRectMake(0, 0, 30, 23);
//    //    [rightButton setImage:[UIImage imageNamed:@"公用_完成"] forState:UIControlStateNormal];
//    [rightButton setTitle:@"更多" forState:UIControlStateNormal];
//    rightButton.titleLabel.font = [UIFont systemFontOfSize:13];
//    [rightButton addTarget:self action:@selector(handleRightButtonAction) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

/**
 *  返回主页面
 */
- (void)handleLeftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 *  完成(返回并传值)
 */
- (void)handleRightButtonAction{
    
}

- (void)loadData
{
    [self showActionView];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    NSString *isFromMyFocuse = [NSString stringWithFormat:@"collection/list/"];
        [dict setObject:@"5" forKey:@"type"];
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,isFromMyFocuse] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        [self hiddenActionView];
        if ([statusString isEqualToString:@"0"]) {
//            [self.drugTableView footerEndRefreshing];
        }else{
            NSArray *jsonDictArr = [[responseObject objectForKey:@"data"] isEqual:[NSNull null]]?nil:[responseObject objectForKey:@"data"];
            
            MyNSLog("%@", jsonDictArr);
            if (jsonDictArr!=nil) {
                for (NSDictionary * dic in jsonDictArr){
                    [self.encyArray addObject:dic];
                }
//                [self.drugTableView reloadData];
            }
        }
        [self.drugTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.encyArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (kScreenWidth == 414) {
        return 54;
    }else{
        return 44;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [self.encyArray[indexPath.row] objectForKey:@"drugname"];
    cell.textLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DrugDetailViewController * circleSectionGradeViewController = [[DrugDetailViewController alloc]init];
    circleSectionGradeViewController.catid = [[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"drugname"];
        circleSectionGradeViewController.catname = [[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"drugid"];
        
        DrugDViewController *drugVC = [[DrugDViewController alloc] init];
        drugVC.drugid = circleSectionGradeViewController.catname;
        drugVC.drugname = circleSectionGradeViewController.catid;
        [self.navigationController pushViewController:drugVC animated:YES];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
